The fractional part of value, with the whole number simply dropped - 3.75 becomes 0.75, and -3.75 becomes -0.75 (the sign stays, never rounded).
frac_part(value) -> real| Name | Type |
|---|---|
value | real |
real
The fractional part of value, with the whole number simply dropped - 3.75 becomes 0.75, and -3.75 becomes -0.75 (the minus stays, and nothing is rounded up or down).
It is the twin of int_part. int_part keeps the whole number and throws the fraction away; frac_part keeps exactly what int_part threw away. So the two always add back up to the number you started with: int_part(value) + frac_part(value) is value, for a plus number and a minus number alike.
| Value | int_part | frac_part |
|---|---|---|
3.75 | 3 | 0.75 |
-3.75 | -3 | -0.75 |
5 | 5 | 0 |
real is a fixed-point number, not a floating-point one, so this is exact - nothing is rounded.
var distance_travelled = 27.6;
var whole_steps = int_part(distance_travelled);
var part_step = frac_part(distance_travelled);whole_steps is 27 and part_step is about 0.6 - how far through the 28th step the object is.
Because frac_part follows the number's sign, it is negative for a negative number. That is what makes it pair with int_part, and it is not what you want for "how far past the edge of its tile is this position", where the answer should never be below zero. For that, take away the rounded-down whole number instead:
var past_edge = x - floor(x);past_edge is 0.25 for an x of -3.75, where frac_part(-3.75) is -0.75. floor always rounds down, toward the smaller number, so what is left over is never negative.
A real counts in steps of 1/4096, so the answer is always a whole number of those steps - and the number you typed may not be one of them. frac_part(1.9) is 0.8999, not 0.9, because 1.9 itself is held as 1.8999. Numbers has the full story.
A couple of instructions. Measured at about 35 cycles a call, counting the loop around it - cheap enough for every frame.
int_part’s twin, and exact: int_part(value) + frac_part(value) is always value again. Both treat a negative number the same way - toward zero - so frac_part(-3.75) is -0.75, not the 0.25 you would get from value - int_floor(value). For "how far past the cell edge is this position", which should never be negative, write value - floor(value) instead. real counts in steps of 1/4096, so the answer is always a whole number of those steps: frac_part(1.9) is 0.8999, not 0.9, because 1.9 itself is held as 1.8999.